//app.js
import config from './config/config.js'
import req from './utils/request.js'
import {
  updateUserTaskComplete, getIndexPage
} from './utils/service.js'
var common = require('./utils/common');

//app.js
App({

  onLaunch: function(options) {
    //初始化网络配置
    req.baseUrl(config.url.baseUrl)
      .header({
        'Accept': 'application/json,text/plain,*/*',
        'content-type': 'application/x-www-form-urlencoded'
      })
      .token('xxxxxxxx')
      .interceptor(res => {
        var result = {}
        if (res && res.statusCode >= 200 && res.statusCode < 300) {
          result.data = res.data
        } else {
          result.error = {
            errorCode: res.statusCode,
            errorMessage: statusMap[res.statusCode + ''] || ''
          }
        }
        return result
      })
      .interceptor(data => {
        var result = {}
        if (data && data.result) {
          result.data = data.data
        } else {
          result.error = {
            errorCode: data.errorCode,
            errorMessage: data.errorMessage
          }
        }
        return result
      })
    common.updateUserTaskComplete(options);
    this.getAppInfo();
  },
  getAppInfo: function () {

    var that = this;
    var appId = config.appId;
    getIndexPage({
      appid: appId
    }).then(res => {
      debugger
      console.log(res);
      if (res.pageType == 2) {
        wx.setNavigationBarTitle({
          title: res.title//页面标题为路由参数
        })
        if (res.jumpType != '') {
          if (res.defaultJumpType == 1 && res.jumpType == 1) {
            common.navigateToH5(res.h5Url);
          }
          else if (res.defaultJumpType == 1 && res.jumpType == 2) {
            common.navigateToMiniProgram(res.jumpAppId, res.appUrl);
          }
        }
      } else if (res.pageType == 1) {
        wx.redirectTo({
          url: '../singlePage/singlePage?title=' + res.title,
        })
      } else {
        wx.redirectTo({
          url: res.url,
        })
      }
    }).catch(e => {

    })
  },
  globalData: {
    userInfo: null, 
  }
})